<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>C/C++ development in Eclipse IDE</title>
<link rel="stylesheet" href="../format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="C/C++, Eclipse IDE">
<meta name="description" content="C/C++ development in Eclipse IDE">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

</head>


<body>

<div class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewVertical */
google_ad_slot = "4231459001";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>


<h1 class="header">C/C++ development in Eclipse IDE</h1>


<h2>Eclipse IDE</h2>

<p>
Eclipse IDE (Integrated Development Environment) is one of the top 
Java IDEs. It competes heavily with another widely used IDE called 
Netbeans. Both Eclipse and Netbeans are primarily Java IDEs. 
Recently they have added support for
other languages. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Eclipse was originally developed by the IBM corporation. Now it is 
maintained by the Eclipse Foundation. Eclipse is a multiplatform,
open source, free of charge IDE. It is a choise of thousands of developers worldwide. 
</p>

<p>
Eclipse IDE is coded in Java programming language. The GUI is created 
using the <b class="keyword">SWT</b>, Standard Widget Toolkit. The SWT 
toolkit comes from IBM too. Unlike Swing, which is written in Java, the 
SWT  lets the underlying OS to create the GUI. SWT uses the java native 
interface to do the job. The main advantages of the SWT are speed and 
native look and feel.
</p>

<p>
The C development environment is available as a plugin or as a standalone 
package. There are several packages of Eclipse IDE available. For 
the C/C++ development, we choose the Eclipse IDE for C/C++ Developers 
from the downloads page of the <a href="http://eclipse.org">eclipse.org</a> site.
</p>



<h2>The IDE</h2>

<p>
The following screenshot shows the Eclipse IDE.
</p>

<br>
<img src="../images/eclipse.png" alt="Eclipse IDE">
<div class="figure">Figure: Eclipse IDE</div>
<br>

<p>
The current Eclipse running is called the <b class="keyword">Workbench</b>. A Workbench is divided into parts calles
<b class="keyword">Perspectives</b>. A Perspective is a logical group of certain views and editors. 
</p>

<ol>
<li>Project Explorer view</li>
<li>Source editor window</li>
<li>Console view</li>
</ol>


<p>
In the Project Explorer view, we create and manage our software projects. 
In the Source editor window, we edit our source, metafile, makefile and other 
editable files. The Eclipse IDE uses the Console view for showing various 
messages or outputs from console applications. 
</p>


<h2>Console C project</h2>

<p>
Next we will create a simple C console application. To create an application 
in Eclipse, we must start a <b class="keyword">project</b>. A project is a 
group of directories, files and other resources needed in a C application. 
</p>

<p> 
From the File menu we select New, C Project. We can also create the project by 
using the context menu, right click on the Project Explorer view and select 
New, C Project from the pop up window. We can also click on the New button 
on the toolbar and select C Project. 
</p>

<br>
<img src="../images/eclipseproject.png" alt="C Project">
<div class="figure">Figure: C Project</div>
<br>


<p>
In the dialog window, we provide a name for our C project. And click on the Finish button.
</p>


<br>
<img src="../images/project.png" alt="Project">
<div class="figure">Figure: Project</div>
<br>

<p>
In the Project Explorer view, we see our newly created C Project. 
The next step is to create a C file. 
We right click on the project in the Project Explorer, 
select New, Source file. 
</p>


<br>
<img src="../images/eclipsesourcefile.png" alt="C Source file">
<div class="figure">Figure: C Source file</div>
<br>


<p>
In the dialog window, we provide a name for our C Source file. 
We must give it a .c extension.  
We type in the following C code:
</p>

<pre class="code">
#include &lt;stdio.h&gt;

int main(int argc, char **argv) {

  printf("A console program.");

  return 0;
}
</pre>


<p>
Finally, we build and run the console application. To build the project, we go to 
the Project menu and select Build Project option or right click on the console 
project in the Project Explorer and select Built Project option. If all went OK, 
we can run the application with the Ctrl + F11 shortcut. 
</p>

<p>
In the Console View, we can see the "A console program." string. 
</p>



<br>
<hr style="width:60%">
<br>


<h2>Simple GTK+ application</h2>

<p>
In the next example, we will create a simple GTK+ application in C language. 
</p>

<p>
In order to create even a simple GTK+ application, we must use a quite a 
few include files and libraries.
Fortunately, there is one handy tool called <b>pkg-config</b>, which 
will greatly help us setting up project settings.
</p>

<p>
The pgk-config returns metadata about installed libraries. Simply put, 
if we want to use a specific library, it will 
provide us necessary dependent libraries and include files, that we need. 
The pkg-config program retrieves information
about packages from special metadata files. 
</p>

<pre class="code">
$ pwd
/usr/lib/pkgconfig
$ cat cairo.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: cairo
Description: Multi-platform 2D graphics library
Version: 1.4.2

Requires.private: freetype2 >= 8.0.2 fontconfig libpng12 xrender >= 0.6 x11 
Libs: -L${libdir} -lcairo
Libs.private: -lz -lm
Cflags: -I${includedir}/cairo
</pre>

<p>
Here is the metadata file for cairo library, a multiplatform 2D 
graphics library. In order to use cairo library, we need
freetype2, fontconfig, libpng12, xrender and x11 libraries. 
</p>

<pre class="code">
$ pkg-config --cflags gtk+-2.0
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 
-I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 
-I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng12
</pre>

<p>
By providing the --cflags option, we get all necessary include 
files for a GTK+ 2.0 application.
</p>

<pre class="code">
$ pkg-config --libs gtk+-2.0
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 
-lfontconfig -lXext -lXrender -lXinerama -lXi -lXrandr 
-lXcursor -lXfixes -lpango-1.0 -lcairo -lX11 -lgobject-2.0 
-lgmodule-2.0 -ldl -lglib-2.0 
</pre>

<p>
Similarly, we get all libraries with the --libs option. Notice that we 
might not need all libs. The math library (-lm)
is not needed, unless we do some mathematical computations.
</p>


<pre class="code">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;gtk/gtk.h&gt;

/* 
 * simple gtk application 
 * author jan bodnar
 * date July 1, 2008
 */

int main(int argc, char** argv) {

    GtkWidget *window;
    gtk_init(&amp;argc, &amp;argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
    gtk_window_set_title(GTK_WINDOW(window), "gtkapp");
    gtk_widget_show(window);

    g_signal_connect(window, "destroy",
		    G_CALLBACK (gtk_main_quit), NULL);
 
    gtk_main();

    return (EXIT_SUCCESS);
}
</pre>


<p>
We create a small window in GTK+. Before we can build and run the 
application, we must set up our project settings. 
We must specify the include files and libraries, that are needed to 
build the application.
</p>

<p>
To provide necessary include directories, right click on the project 
icon and select properties. Expand C/C++ General. 
Select Paths and Symbols. Click on the Includes tabs. From the 
languages group, click on the GNU C. 
Add necessary paths to the include directories. From the pkg-config
 tool, we got the list of the directories.
</p>

<br>
<img src="../images/eclipseincludes.png" alt="Include directories">
<div class="figure">Figure: Include directories</div>
<br>

<p>
Next we need to include necessary libraries. For our application, 
we need to add only the /usr/lib/libgtk-x11-2.0.so dynamic library. 
Select Project Properties. Expand the C/C++ Build tab. Select settings. 
From the Tool Settings tab, expand the GCC C Linker option. 
Click on libraries. Add the /usr/lib/libgtk-x11-2.0.so 
to the Libraries window. Notice, that this path may be 
different on your system. 
</p>

<br>
<img src="../images/eclipselibraries.png" alt="Libraries">
<div class="figure">Figure: Libraries</div>
<br>


<p>
We build the project and run it. We should see a small GTK+ window on the screen. 
</p>

<br>
<img src="../images/gtkapp.png" alt="GTK+ application">
<div class="figure">Figure: GTK+ application</div>
<br>

<br>
<hr style="width:60%">
<br>



<h2>Final word</h2>

<p>
Eclipse IDE provides excellent tools for C/C++ development that makes 
the programming more efficient. UNIX/Linux provides a plethora of 
development tools for C/C++ projects. Some developers prefer the traditional
Vi/Emacs/command line tools. Some prefer modern IDEs. 
</p>

<p>
When creating large projects, IDEs like Eclipse gain momentum. Eclipse 
provides lots of features like code completion, code folding, syntax 
highlighting, code templates, integrated compiler and debugger. Just to name a few. 
Especially, it is much easier for a human to cope with multiple files 
when usign a GUI, than with command line tools
or traditional non GUI editors. Finally, choosing the tool for programming 
has always been a matter of subjective preference.
</p>

<br>
<img src="../images/eclipsecodehighlight.png" alt="Syntax highlighting in Eclipse">
<div class="figure">Figure: Syntax highlighting in Eclipse</div>
<br>


<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified July 2, 2008  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</body>
</html>

